package Year22_Month04.day22;

public class 相交链表 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        int count = 0;
        ListNode tmpA = headA;//定义指针A
        ListNode tmpB = headB;//定义指针B
        while(tmpB != tmpA){
            if(tmpB.next == null || tmpA.next == null){
                count++;
            }
            tmpB = tmpB.next == null ? headA : tmpB.next;
            tmpA = tmpA.next == null ? headB : tmpA.next;
            if(count == 3){
                break;
            }
        }
        if(count == 3){
            return null;
        }
        return tmpA;
    }
}
